package com.madarasan.xml;

import java.net.URL;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.madarasan.SkiDroid.MyApplication;
import com.madarasan.SkiDroid.R;
import com.madarasan.notneeded.Customer;
import com.madarasan.notneeded.CustomersList;
import com.madarasan.resorts.ShowResortGeneralInfo;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class MessageList extends ListActivity {
	
	private List<Message> messages;
	private ProgressDialog dialog;
	
	
	/*public class RssLoadingTask extends AsyncTask<Void, Void, Void> {
		
		

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			Log.i("MessageList","arrived in RssLoadingTask on post execute. Calling loadfeed");
			loadFeed();
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			//preReadRss();
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			// TODO Auto-generated method stub
			//super.onProgressUpdate(values);
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			// TODO Auto-generated method stub
			Log.i("MessageList","arrived in RssLoadingTask do in Background. Calling loadfeed");
			//loadFeed();
			return null;
		}

	}*/
	
	
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        //testResponse();
        setContentView(R.layout.cities);
        Log.i("MessageList","called loadfeed");
        loadFeed();
        
        
        //startReadRss();
        
        
        //dialog = ProgressDialog.show(MessageList.this, "Loading", "Loading, please wait..");
        
        
        //Partially worked (no error on emulator still fails on SNS)
       /*(new Thread(new Runnable() {
            @Override
            public void run() {
            	loadFeed();
            }
        })).start();*/
        
        
        
    }

    
    
    
	public void loadFeed()  {
    	try{
	    	BaseFeedParser parser = new BaseFeedParser();
	    	messages = parser.parse();
	    	List<String> titles = new ArrayList<String>(messages.size());
	    	for (Message msg : messages){
	    		titles.add(msg.getTitle());
	    	}
	    	ArrayAdapter<String> adapter = 
	    		new ArrayAdapter<String>(this, R.layout.row,titles);
	    	this.setListAdapter(adapter);
	    	
	    	    	
	    	final ListView lv = getListView();
	    	lv.setItemsCanFocus(false);
	    	lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);   
    	} catch (Throwable t){
    		Log.e("MessageList loadFeed",t.getMessage(),t);
    	}
    }
    
    
    /*
     * public void loadFeed()  {
     
    	try{
    		
    		
    		runOnUiThread(new Runnable() {
    		     public void run() {

    		    	 BaseFeedParser parser = new BaseFeedParser();
    			    	messages = parser.parse();
    			    	List<String> titles = new ArrayList<String>(messages.size());
    			    	for (Message msg : messages){
    			    		titles.add(msg.getTitle());
    			    		Log.i("MessageList->LoadFeed", "Passing here "+ msg.getTitle());
    			    	}
    			    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.row,titles);
    			    	setListAdapter(adapter);
    			    	final ListView lv = getListView();
    			    	lv.setItemsCanFocus(false);
    			    	lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);   

    		    }
    		});
    		
	    	
	    	
	    	    	
	    	
    	} catch (Throwable t){
    		Log.e("MessageList loadFeed",t.getMessage(),t);
    	}
    }*/
	
	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		/*Intent viewMessage = new Intent(Intent.ACTION_VIEW, 
				Uri.parse(messages.get(position).getLink().toExternalForm()));
		this.startActivity(viewMessage);*/
		
		
		Intent ShowResortGenInfoIntent = new Intent(this, ShowResortGeneralInfo.class);
		
		
		String title = messages.get(position).getTitle().toString();
		String description = messages.get(position).getDescription().toString();
		String tempMax = messages.get(position).getTempMax().toString();
		String tempMin = messages.get(position).getTempMin().toString();
		String wind = messages.get(position).getWind().toString();
		String snow = messages.get(position).getSnow().toString();
		String weather = messages.get(position).getWeather().toString();
		String link = messages.get(position).getLink().toString();
		String date = messages.get(position).getDate();
		
		
	
		
		
		//Log.i("MessageList inainte", ((MyApplication) this.getApplication()).getResortSelected().toString());
		
		((MyApplication) this.getApplication()).MyApplication(title, link, description, tempMax, tempMin, wind, snow, weather, date);
		((MyApplication) this.getApplication()).setResortSelected(true);
		
		//Log.i("MessageList dupa", ((MyApplication) this.getApplication()).getResortSelected().toString());
		
		
		
		
		//This is new code added here!
		
		//Create and populate the list
		 
        CustomersList customers = new CustomersList();
 
        Customer c1 = new Customer(title, link, description, tempMax, tempMin, wind, snow, weather, date);
        customers.add(c1);
 
        
		
    	
    	Bundle b = new Bundle();
    	b.putParcelable("customers", customers);
    	ShowResortGenInfoIntent.putExtras(b);
    	
    	startActivity(ShowResortGenInfoIntent);
		
		
	}
	
	public void testResponse(CharSequence text) { 
    	Context context = getApplicationContext();
    	int duration = Toast.LENGTH_SHORT;

    	Toast toast = Toast.makeText(context, text, duration);
    	toast.show();
    }
    
}